package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.entity.Sku;
import cn.tedu.csmall.product.pojo.param.SkuAddNewParam;
import cn.tedu.csmall.product.pojo.vo.SkuListItemVO;
import cn.tedu.csmall.product.pojo.vo.SpuListItemVO;
import cn.tedu.csmall.product.pojo.vo.SpuStandardVO;
import cn.tedu.csmall.product.service.SkuService;
import cn.tedu.csmall.product.util.IdUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SkuServiceImpl implements SkuService {
    @Autowired
    SkuMapper skuMapper;
    @Autowired
    SpuMapper spuMapper;
    @Autowired
    IdUtils idUtils;

    @Override
    public PageData<SkuListItemVO> listBySpuId(Long spuId) {
        log.debug("开始处理【spu查询sku列表】的业务,参数：{}",spuId);
        PageHelper.startPage(1,Integer.MAX_VALUE);
        List<SkuListItemVO> list = skuMapper.listBySpuId(spuId);
        PageInfo<SkuListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SkuListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public void addNew(SkuAddNewParam skuAddNewParam) {
        log.debug("开始处理【新增SKU】的业务,参数：{}",skuAddNewParam);
        QueryWrapper<Sku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",skuAddNewParam.getTitle());
        int countByTitle = skuMapper.selectCount(queryWrapper);
        if (countByTitle > 0){
            String message = "新增sku失败,标题名称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查spu: 是否存在
        Long spuId = skuAddNewParam.getSpuId();;
        SpuStandardVO spuStandardVO = spuMapper.getStandardById(spuId);
        if (spuStandardVO == null){
            String message = "新增sku失败,所属spu不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Sku sku = new Sku();
        BeanUtils.copyProperties(skuAddNewParam,sku);

        Long skuId = idUtils.generateSpuId();
        sku.setId(skuId);
        sku.setAttributeTemplateId(spuStandardVO.getAttributeTemplateId());
        sku.setAlbumId(spuStandardVO.getAlbumId());
        sku.setPictures(spuStandardVO.getPictures());
        sku.setSales(0);
        sku.setCommentCount(0);
        sku.setPositiveCommentCount(0);

        //插入SKU数据
        int rows = skuMapper.insert(sku);
        if (rows != 1){
            String message = "新增sku失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
}
